﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
namespace HF.Permission.Data
{
    /// <summary>
    /// 数据访问类OperationData。
    /// </summary>
    public class OperationData
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "OperationId", "OperationName", "OperationAlias", "OperationFlag", "OperationDes" };
        private string tableName = "HF_Operation";
        private string keyField = "OperationId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(OperationEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@OperationId", entity.OperationId, typeof(string));
            sqlDataItem.AppendParameter("@OperationName", entity.OperationName, typeof(string));
            sqlDataItem.AppendParameter("@OperationAlias", entity.OperationAlias, typeof(string));
            sqlDataItem.AppendParameter("@OperationFlag", entity.OperationFlag, typeof(bool));
            sqlDataItem.AppendParameter("@OperationDes", entity.OperationDes, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public OperationEntity GetOperationEntity(DataRow dr)
        {
            OperationEntity entity = new OperationEntity();
            string value = "";
            entity.OperationId = dr["OperationId"].ToString();
            entity.OperationName = dr["OperationName"].ToString();
            entity.OperationAlias = dr["OperationAlias"].ToString();
            value = dr["OperationFlag"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.OperationFlag = true;
                }
                else
                {
                    entity.OperationFlag = false;
                }
            }
            entity.OperationDes = dr["OperationDes"].ToString();
            return entity;
        }

        #endregion
        #region  构造函数
        public OperationData()
        { }
        public static OperationData GetInstance()
        {
            return new OperationData();
        }
        #endregion
        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(OperationEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(OperationEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string operationId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, operationId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="operationId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string operationId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, operationId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="OperationId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetOperationTable(string operationId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, operationId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="operationId">主键Id</param>
        /// <returns>DataTable</returns>
        public OperationEntity GetOperationEntity(string operationId)
        {
            DataTable dt = GetOperationTable(operationId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetOperationEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion
        #region  扩展方法
        //此处添加扩展方法
        /// <summary>
        /// 获取所有操作
        /// </summary>
        /// <returns></returns>
        public  DataTable GetAllOperations()
        {
            try
            {
                string tmpSql = "select * from HF_Operation";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 获得功能中的所有操作
        /// </summary>
        /// <param name="functionId">功能Id</param>
        /// <returns>DataTable</returns>
        public  DataTable GetOperationsByFunction(string functionId)
        {
            try
            {
                string tmpSql = "select * from HF_Operation where left(OperationId,4)=@functionId order by OperationId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.AppendParameter("@functionId", functionId);
                sqlItem.CommandText = tmpSql;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 获得功能中具有访问权限的操作
        /// </summary>
        /// <param name="functionId">功能Id</param>
        /// /// <param name="groupId">权限组Id</param>
        /// <returns>DataTable</returns>
        public  DataTable GetPowerOperations(string functionId, string groupId)
        {
            try
            {
                string tmpSql = "select distinct o.OperationId,o.OperationName,o.OperationDes,o.OperationAlias,o.OperationFlag from HF_Purview " +
                                                   " p left join HF_Operation o on p.operationId=o.operationId" +
                                                   " where left(p.operationId,4)=@functionId and p.groupId=@groupId ";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.AppendParameter("@functionId", functionId);
                sqlItem.AppendParameter("@groupId", groupId);
                sqlItem.CommandText = tmpSql;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 获得功能下最大的操作编号，取最后两位
        /// </summary>
        /// <param name="functionId">4位功能编号</param>
        /// <returns>2位操作编号</returns>
        public string GetMaxOperationId(string functionId)
        {
            string tmpSql = "select right(OperationId,2) OperationId from HF_Operation where left(OperationId,4)=@functionId order by OperationId desc ";
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.AppendParameter("@functionId", functionId);
            sqlItem.CommandText = tmpSql;
            ClientDBAgent agent = new ClientDBAgent();
            string MaxId = agent.ExecuteScalar(sqlItem);
            if (string.IsNullOrEmpty(MaxId))
            {
                MaxId = "00";
            }
            return MaxId;
        }
        /// <summary>
        /// 获取所有操作
        /// </summary>
        /// <returns></returns>
        public List<OperationEntity> GetAllOperationEntitys()
        {
            try
            {
                List<OperationEntity> result = new List<OperationEntity>();
                DataTable dt = GetAllOperations();
                foreach (DataRow dr in dt.Rows)
                {
                    OperationEntity entity = GetOperationEntity(dr);
                    result.Add(entity);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 获得功能中的所有操作
        /// </summary>
        /// <param name="functionId">功能Id</param>
        /// <returns>Entity</returns>
        public List<OperationEntity> GetOperationEntitysByFunction(string functionId)
        {
            try
            {
                List<OperationEntity> result = new List<OperationEntity>();
                DataTable dt = GetOperationsByFunction(functionId);
                foreach (DataRow dr in dt.Rows)
                {
                    OperationEntity entity = GetOperationEntity(dr);
                    result.Add(entity);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 获得功能中具有访问权限的操作
        /// </summary>
        /// <param name="functionId">功能Id</param>
        /// <param name="groupId">权限组Id</param>
        /// <returns>Entitys</returns>
        public List<OperationEntity> GetPowerOperationEntitys(string functionId, string groupId)
        {
            try
            {
                List<OperationEntity> result = new List<OperationEntity>();
                DataTable dt = GetPowerOperations(functionId,groupId);
                foreach (DataRow dr in dt.Rows)
                {
                    OperationEntity entity = GetOperationEntity(dr);
                    result.Add(entity);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion
        #endregion
    }
}

